package com.tuanzi.loan.business.repository.family.impl;

import com.tuanzi.loan.business.repository.family.custom.StatNewCreditCardUvRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepositoryPlus;
import com.tuanzi.loan.vo.request.family.StatNewCreditCardUvRequest;
import com.tuanzi.loan.vo.response.family.StatNewCreditCardUvResponse;
import org.apache.commons.lang3.StringUtils;
import org.jooq.Record;
import org.jooq.RecordMapper;
import org.jooq.SelectConditionStep;
import org.jooq.impl.DSL;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import java.util.List;

import static com.tuanzi.loan.generated.jooq.stat.tables.StatNewCreditCardUv.STAT_NEW_CREDIT_CARD_UV;

/**
 * User: liangZongXi
 * Create time: 18:19 2018/9/6
 * Description:
 */
@Repository
public class StatNewCreditCardUvRepositoryImpl extends JOOQBaseRepositoryPlus implements StatNewCreditCardUvRepositoryCustom{
    @Override
    public Page<StatNewCreditCardUvResponse> list(StatNewCreditCardUvRequest searchRequest, Pageable pageable) {
        fields(STAT_NEW_CREDIT_CARD_UV.fields());

        SelectConditionStep<Record> sql = dslContext.select(STAT_NEW_CREDIT_CARD_UV.fields())
                .from(STAT_NEW_CREDIT_CARD_UV)
                .where(DSL.trueCondition());

        if(null != searchRequest.getBeginDate()){
            sql.and(STAT_NEW_CREDIT_CARD_UV.INTO_DATE.ge(new java.sql.Date(searchRequest.getBeginDate().getTime())));
        }

        if(null != searchRequest.getEndDate()){
            sql.and(STAT_NEW_CREDIT_CARD_UV.INTO_DATE.le(new java.sql.Date(searchRequest.getEndDate().getTime())));
        }

        if(StringUtils.isNotBlank(searchRequest.getPrdid())){
            sql.and(STAT_NEW_CREDIT_CARD_UV.PRDID.eq(searchRequest.getPrdid()));
        }

        if(StringUtils.isNotBlank(searchRequest.getEntrance())){
            sql.and(STAT_NEW_CREDIT_CARD_UV.ENTRANCE.eq(searchRequest.getEntrance()));
        }

        if(StringUtils.isNotBlank(searchRequest.getChannel())){
            sql.and(STAT_NEW_CREDIT_CARD_UV.CHANNEL.eq(searchRequest.getChannel()));
        }
        if(StringUtils.isNotBlank(searchRequest.getUserType())){
            sql.and(STAT_NEW_CREDIT_CARD_UV.USER_TYPE.eq(searchRequest.getUserType()));
        }
        List<String> nameList = searchRequest.getNameList();
        if(null != nameList && nameList.size() >0){
            sql.and(STAT_NEW_CREDIT_CARD_UV.CARD_NAME.contains(nameList.get(0)));
            if(nameList.size() >1){
                for (int i=1;i<nameList.size();i++){
                    sql.or(STAT_NEW_CREDIT_CARD_UV.CARD_NAME.contains(nameList.get(i)));
                }
            }
        }

        return search(sql.getQuery(),pageable, new ResponseRecord());
    }

    @Override
    protected TableImpl getTable() {
        return STAT_NEW_CREDIT_CARD_UV;
    }


    private final class ResponseRecord implements RecordMapper<Record, StatNewCreditCardUvResponse>{

        @Override
        public StatNewCreditCardUvResponse map(Record record) {
            StatNewCreditCardUvResponse response = new StatNewCreditCardUvResponse();
            response.setId(record.get(STAT_NEW_CREDIT_CARD_UV.ID));
            response.setIntoDate(record.get(STAT_NEW_CREDIT_CARD_UV.INTO_DATE));
            response.setCardName(record.get(STAT_NEW_CREDIT_CARD_UV.CARD_NAME));
            response.setPrdid(record.get(STAT_NEW_CREDIT_CARD_UV.PRDID));
            response.setChannel(record.get(STAT_NEW_CREDIT_CARD_UV.CHANNEL));
            response.setUserType(record.get(STAT_NEW_CREDIT_CARD_UV.USER_TYPE));
            response.setEntrance(record.get(STAT_NEW_CREDIT_CARD_UV.ENTRANCE));
            response.setHomeUv(record.get(STAT_NEW_CREDIT_CARD_UV.HOME_UV));
            response.setDetailUv(record.get(STAT_NEW_CREDIT_CARD_UV.DETAIL_UV));
            response.setClickApplyUv(record.get(STAT_NEW_CREDIT_CARD_UV.CLICK_APPLY_UV));
            response.setRetCashUv(record.get(STAT_NEW_CREDIT_CARD_UV.RET_CASH_UV));
            return response;
        }
    }
}
